/**
 * 
 * @purpose: ELEMENT EASE CONTROLLER...
 * @auth: Ning
 * @date: 2009-8-20
 * 
 * */
package core.ease
{
	import core.ease.EaseFade;
	import core.ease.EaseFadeEvents;
	
	import flash.display.FrameLabel;
	import flash.display.MovieClip;

	public class MotionController extends EaseFade
	{
		//START...
		
		//PROPERTY...
		private var tar:MovieClip;
				
		
		//CONSTRUCTOR...
		public function MotionController(target:MovieClip)
		{
			
			super(target);
			
			tar = target;
				
			init();			
			
		}
		
		//METHODS...
		
		//init...
		private function init():void
		{
			
			tar.stop();
			
			var labels:Array = tar.currentLabels;
			
			labels.sortOn("frame",Array.NUMERIC);
															
			var label:FrameLabel;
			
			var prevLabel:FrameLabel;
			
			var fun:Function;
			
			for(var i:uint = 0;i < labels.length;i++)
			{
				
				label = labels[i] as FrameLabel;
				
				prevLabel = labels[i == 0 ? labels.length - 1 : i - 1] as FrameLabel;	
									
				switch(prevLabel.name)
				{
					
					case EaseFade.DISPLAY:
					
					fun = onDisplay;
					
					break;
						
					case EaseFade.EASE_IN:
					
					fun = onEaseInOver;
					
					break;
					
					case EaseFade.EASE_OUT:
					
					fun = onEaseOutOver;				
					
					break;
					
				}
				
				tar.addFrameScript((label.frame - 1 < 1 ? tar.totalFrames : label.frame - 1) - 1,fun);
																				
			}
							
		}
		
		
		//on ease in over...
		private function onEaseInOver():void
		{
			
			tar.stop();
																					
			dispatchEvent(new EaseFadeEvents(EaseFadeEvents.EASE_IN_OVER));
						
		}
		
		//on ease out over...
		private function onEaseOutOver():void
		{
			
			tar.stop();
												
			dispatchEvent(new EaseFadeEvents(EaseFadeEvents.EASE_OUT_OVER));
						
		}
		
		//on display...
		private function onDisplay():void
		{
			
			tar.stop();
																		
			dispatchEvent(new EaseFadeEvents(EaseFadeEvents.READY_FOR_DISPLAY));
					
			
		}
		
		
		
		//overridee ease in...
		public override function easeIn():void
		{
			
			tar.gotoAndPlay(EaseFade.EASE_IN);
			
			
		}
		
		//override ease out...
		public override function easeOut():void
		{
			
			tar.gotoAndPlay(EaseFade.EASE_OUT);
			
		}
		
		//override display...
		public override function display():void
		{
			
			tar.gotoAndPlay(EaseFade.DISPLAY);
			
		}
		
		
		//END...		
	}
}